/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Reservation.java
 *
 * Created on Mar 22, 2010, 4:15:49 PM
 */

package Library.GUI;

import SourceCode.*;
import SourceCode.Copy;
import SourceCode.Item;
import SourceCode.LibraryGeneral;
import SourceCode.Magazine;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author ssaad
 */
public class Reservation_Panel extends javax.swing.JPanel {


    /*
     * type =0 not book not magazine
     * type = 1 magazine
     * type = 2 book
     */
    private int type =0;
    /** Creates new form Reservation */
    public Reservation_Panel() {
        initComponents();
      
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtMember = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        txtIssueNo = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        btnSrchMagazineCopy = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        btnSrchBookCopy = new javax.swing.JButton();
        txtISBN = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        btnReserve = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        itemcopylist = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        membercopylist = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        jLabel1.setText("Member ID:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtMember, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(147, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtMember, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        txtIssueNo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIssueNoActionPerformed(evt);
            }
        });
        txtIssueNo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtIssueNoKeyPressed(evt);
            }
        });

        jLabel3.setText("Issue No.");

        btnSrchMagazineCopy.setText("Search");
        btnSrchMagazineCopy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSrchMagazineCopyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(txtIssueNo, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 59, Short.MAX_VALUE)
                .addComponent(btnSrchMagazineCopy, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSrchMagazineCopy)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtIssueNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Magazine", jPanel3);

        btnSrchBookCopy.setText("Search");
        btnSrchBookCopy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSrchBookCopyActionPerformed(evt);
            }
        });

        jLabel4.setText("ISBN");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(txtISBN, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 59, Short.MAX_VALUE)
                .addComponent(btnSrchBookCopy, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtISBN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSrchBookCopy))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Book", jPanel2);

        jButton2.setText("Close");

        btnReserve.setText("Reserve");
        btnReserve.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReserveActionPerformed(evt);
            }
        });

        itemcopylist.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "CopyID", "Title", "Availability", "return date"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane2.setViewportView(itemcopylist);

        membercopylist.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title", "CheckOut Days", "ReserveDate", "Available On"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane3.setViewportView(membercopylist);

        jLabel2.setFont(new java.awt.Font("Tahoma", 2, 12));
        jLabel2.setText("Item Copy Ststus");

        jLabel5.setFont(new java.awt.Font("Tahoma", 2, 12));
        jLabel5.setText("Member Reserved Item");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnReserve, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 482, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 472, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 472, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 239, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 472, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addGap(4, 4, 4)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5))
                    .addComponent(btnReserve))
                .addGap(8, 8, 8)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnReserve, jButton2});

    }// </editor-fold>//GEN-END:initComponents

    private void btnSrchMagazineCopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSrchMagazineCopyActionPerformed
        type = 1;
        // TODO add your handling code here:
        String IssueNo = txtIssueNo.getText();
        if(LibraryGeneral.Map_Magazines.containsKey(IssueNo))
        {
            Magazine magazine = LibraryGeneral.Map_Magazines.get(IssueNo);
            SetItemCopy(magazine);
        }
        else
        {
            resetItemCopyList();
            JOptionPane.showMessageDialog(null, "Invalid Magazine Issue No. ");
            
        }
    }//GEN-LAST:event_btnSrchMagazineCopyActionPerformed

    private void btnSrchBookCopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSrchBookCopyActionPerformed
        type = 2;
        // TODO add your handling code here:
        String ISBN = txtISBN.getText();
        if(LibraryGeneral.Map_Books.containsKey(ISBN))
        {
            Book book = LibraryGeneral.Map_Books.get(ISBN);
            SetItemCopy(book);
        }
        else
        {
            resetItemCopyList();
            JOptionPane.showMessageDialog(null, "Invalid Book ISBN ");
            
        }
    }//GEN-LAST:event_btnSrchBookCopyActionPerformed

    private void btnReserveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReserveActionPerformed
        // TODO add your handling code here:
        String Memberid = txtMember.getText();
        int Mid =0;
        String ISBN = txtISBN.getName() ;
        String issueno = txtIssueNo.getText();
        Reservation reserve_item;
        //check availability
        if(Memberid.length() <=0 )
        {
            JOptionPane.showMessageDialog(null, "Enter Member ID");
            return;
        }


        if(type ==1)
        {
        if(LibraryGeneral.Map_Magazines.containsKey(issueno.trim()) == false )
        {
            JOptionPane.showMessageDialog(null, "Invalid Magazine Issue No. ID");
            return;
        }
        }
        else if (type ==2)
        {
             if(LibraryGeneral.Map_Books.containsKey(ISBN.trim()) == false )
                {
                    JOptionPane.showMessageDialog(null, "Invalid Book ISBN Number");
                     return;
                }

        }
        else
        {
            JOptionPane.showMessageDialog(null, "Enter Book or magazine ID To Reserve");
            return;
        }
        try
        {
            Mid = Integer.parseInt(Memberid);
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Incorrect Member ID Format");
            return;
        }


        //check member existance in the System
        if(LibraryGeneral.Map_Members.containsKey(Mid))
        {
           
            Member member = LibraryGeneral.Map_Members.get(Mid);
            if(type == 1)
            {
                Magazine magazine = LibraryGeneral.Map_Magazines.get(issueno);
                //reserve book
                if(member.reserveItem(magazine) == null)
                {
                     JOptionPane.showMessageDialog(null, "un available reservation there are available Copies of Item");
                     return;
                }
                else
                {
                     JOptionPane.showMessageDialog(null, "Item reserved Succesfully");
                }
               //reserve_item = member.reserveItem(magazine);
            }
            else if(type == 2) // Book
            {
               Book book = LibraryGeneral.Map_Books.get(ISBN);
               //reserve book
                if(member.reserveItem(book) == null)
                {
                     JOptionPane.showMessageDialog(null, "un available reservation there are available Copies of Item");
                    return;
                }
                else
                {
                     JOptionPane.showMessageDialog(null, "Item reserved Succesfully");

                }
               // reserve_item = member.reserveItem(book);
            }

            //update membercopylist
             LoadMemberCopyListTable(member);
          

        }
        else
        {
            JOptionPane.showMessageDialog(null, "Member ID Not exist");
            return;
        }



    }//GEN-LAST:event_btnReserveActionPerformed

    private void txtIssueNoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIssueNoActionPerformed
      JOptionPane.showMessageDialog(null, "Incorrect Member ID Format" + txtMember.getText());
        // TODO add your handling code here:
        /*
         * load MemberCopyList table with all items reserved by member
         */
        String Memberid = txtMember.getText();
        int Mid;
        try
        {
            Mid = Integer.parseInt(Memberid);
            if(LibraryGeneral.Map_Members.containsKey(Mid))
            {
                LoadMemberCopyListTable(LibraryGeneral.Map_Members.get(Mid));
            }
        }
        catch(Exception ex)
        {
            //JOptionPane.showMessageDialog(null, "Incorrect Member ID Format");
            return;
        }
       

    }//GEN-LAST:event_txtIssueNoActionPerformed

    private void txtIssueNoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtIssueNoKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode() == 13)
        {
            String Memberid = txtMember.getText();
        int Mid;
        try
        {
            Mid = Integer.parseInt(Memberid);
            if(LibraryGeneral.Map_Members.containsKey(Mid))
            {
                LoadMemberCopyListTable(LibraryGeneral.Map_Members.get(Mid));
            }
        }
        catch(Exception ex)
        {
            //JOptionPane.showMessageDialog(null, "Incorrect Member ID Format");
            return;
        }
        }
    }//GEN-LAST:event_txtIssueNoKeyPressed

    public void LoadMemberCopyListTable(Member m)
    {
        Object[] rowdata;
       DefaultTableModel defTableModel = new DefaultTableModel();
       defTableModel.addColumn("Title.");
       defTableModel.addColumn("Checkout Days");
       defTableModel.addColumn("Reservation Date");
       defTableModel.addColumn("Available On");

       for(Reservation r : m.reservations)
       {
           rowdata = new Object[]{r.getItem().getTitle(),  r.getItem().getCheckoutlength(), r.getReservationDate(), r.FirstValidDate() };
           defTableModel.addRow(rowdata);
       }
       this.membercopylist.setModel(defTableModel);
    }

    public void resetItemCopyList()
    {
       DefaultTableModel defTableModel = new DefaultTableModel();
       defTableModel.addColumn("Copyid.");
       defTableModel.addColumn("Title");
       defTableModel.addColumn("Availability");
       defTableModel.addColumn("Reurn Date");
       this.itemcopylist.setModel(defTableModel);
}
public void SetItemCopy(Item item )
{
       Object[] rowdata;
       DefaultTableModel defTableModel = new DefaultTableModel();
       defTableModel.addColumn("Copyid.");
       defTableModel.addColumn("Title");
       defTableModel.addColumn("Availability");
       defTableModel.addColumn("Reurn Date");

       for(Copy c : item.Copies)
            {
            String isvalid ="";
                    if(c.IsAvailable())
                         isvalid ="Available;" ;
                    else
                        isvalid ="InAvailable;" ;
                if(c.loan != null)
                    rowdata = new Object[]{c.getClass(),  item.getTitle(), isvalid, c.loan.getReturnDate() };
                else
                    rowdata = new Object[]{c.getClass(),  item.getTitle(), isvalid, null };
                defTableModel.addRow(rowdata);
            }
           
            itemcopylist.setModel(defTableModel);
       
       
       
       
}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnReserve;
    private javax.swing.JButton btnSrchBookCopy;
    private javax.swing.JButton btnSrchMagazineCopy;
    private javax.swing.JTable itemcopylist;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable membercopylist;
    private javax.swing.JTextField txtISBN;
    private javax.swing.JTextField txtIssueNo;
    private javax.swing.JTextField txtMember;
    // End of variables declaration//GEN-END:variables

}
